<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="代运营商编" prop="receiveMerchantNo">
        <el-input
          v-model="queryParams.receiveMerchantNo"
          placeholder="请输入代运营商编"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户商编" prop="contributeMerchantNo">
        <el-input
          v-model="queryParams.contributeMerchantNo"
          placeholder="请输入商户商编"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="绑卡id" prop="bindId">
        <el-input
          v-model="queryParams.bindId"
          placeholder="请输入绑卡id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现id" prop="withHoIdId">
        <el-input
          v-model="queryParams.withHoIdId"
          placeholder="请输入提现id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable >
          <el-option label="处理中" :value="1" />
          <el-option label="抽佣成功" :value="2" />
          <el-option label="抽佣失败" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ecs:withholdingWithhold:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          v-if="withholdList.length === 0"
          @click="handleYeePayWithholdingWithhold"
          v-hasPermi="['ecs:withholdingWithhold:update']"
        >抽佣</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="withholdList" @selection-change="handleSelectionChange">
      <el-table-column label="代运营商编" align="center" prop="receiveMerchantNo" min-width="200">
        <template slot-scope="{ row }">
          {{row.nickName}}
          <br/>
          ({{row.receiveMerchantNo}})
        </template>
      </el-table-column>
      <el-table-column label="商户商编" align="center" prop="contributeMerchantNo"  min-width="200">
        <template slot-scope="{ row }">
          {{row.shortName}}
          <br/>
          ({{row.contributeMerchantNo}})
        </template>
      </el-table-column>
      <el-table-column label="抽佣比例/%" align="center" prop="withholdRatio"   min-width="120"/>
      <el-table-column label="抽佣金额" align="center" prop="withholdAmount"  min-width="150" />
      <el-table-column label="状态" align="center"  min-width="110" prop="isSettlement" >
        <template slot-scope="scope">
          <span v-if="scope.row.status === 1">处理中</span>
          <span v-else-if="scope.row.status === 2">抽佣成功</span>
          <el-popover
            v-else-if="scope.row.status === 3"
            placement="top-start"
            :width="300"
            trigger="click"
            :content="scope.row.failMessage">
            <template #reference>
              <el-tag style="cursor: pointer" slot="reference" type="danger">抽佣失败</el-tag>
            </template>
          </el-popover>
          <span v-else>其它</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right"  min-width="200" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleQueryStatus(scope.row.id)"
            v-hasPermi="['ecs:withholdingRecord:update']"
          >查询状态</el-button>
          <el-button
            size="mini"
            type="text"
            :disabled="scope.row.status !== 3"
            @click="handleYeePayWithholdingWithhold(scope.row.id)"
            v-hasPermi="['ecs:withholdingRecord:update']"
          >抽佣</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listWithholdWithhold, getWithholdWithhold, updateWithholdingWithholdStatus, yeePayWithholdingWithhold } from "@/api/ecs/withholdingWithhold";

export default {
  name: "Withhold",
  props:{
    withdrawId:{
      type:String,
      default:""
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 代运营服务商抽佣表格数据
      withholdList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        requestNo: null,
        receiveMerchantNo: null,
        withholdRatio: null,
        userNo: null,
        contributeMerchantNo: null,
        bindId: null,
        withHoIdId: null,
        userType: null,
        fee: null,
        withholdAmount: null,
        ypSettleAmount: null,
        status: null,
        isSettlement: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.queryParams.withHoIdId = this.withdrawId
    this.getList();
  },
  methods: {
    /** 查询代运营服务商抽佣列表 */
    getList() {
      this.loading = true;
      listWithholdWithhold(this.queryParams).then(response => {
        this.withholdList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        requestNo: null,
        receiveMerchantNo: null,
        withholdRatio: null,
        userNo: null,
        contributeMerchantNo: null,
        bindId: null,
        withHoIdId: null,
        userType: null,
        fee: null,
        withholdAmount: null,
        ypSettleAmount: null,
        createTime: null,
        updateTime: null,
        status: null,
        isSettlement: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加代运营服务商抽佣";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWithholdWithhold(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改代运营服务商抽佣";
      });
    },
    /** 查询状态 */
    handleQueryStatus(id) {
      this.loading = true
      updateWithholdingWithholdStatus(id).then(res=>{
        this.loading = false
        this.getList()
      }).catch(()=>{
        this.loading = false
      })
    },
    /** 发起抽佣 */
    handleYeePayWithholdingWithhold(id) {
      this.loading = true
      yeePayWithholdingWithhold(id).then(res=>{
        this.loading = false
        this.getList()
      }).catch(()=>{
        this.loading = false
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ecs/withhold/export', {
        ...this.queryParams
      }, `withhold_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
